package com.quanquan.user.controller;

import DTO.PageDTO;
import DTO.user.UserDTO;
import base.BaseController;
import com.quanquan.user.entity.UserGroup;
import com.quanquan.user.exception.UserGroupException;
import com.quanquan.user.request.UserGroupRequestBody;
import com.quanquan.user.service.UserGroupService;
import enums.ResCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.StringUtil;
import web.response.ResultBody;

import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: UserGroupController
 * @Author: xiaoqianh丶
 * @Date: 2019-03-19 15:03
 * @Version: V1.0
 */
@RestController
@RequestMapping(value = "/user_group")
public class UserGroupController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());


    private final UserGroupService userGroupService;

    @Autowired
    public UserGroupController(UserGroupService userGroupService) {
        this.userGroupService = userGroupService;
    }

    /**
     * 新建分组
     *
     * @param requestbody
     * @return
     */
    @PostMapping("/add")
    public ResultBody addGroup(@RequestBody UserGroupRequestBody requestbody) {
        String msg = userGroupService.createGroup(requestbody);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), msg);
    }

    @PostMapping("/get")
    public ResultBody get(@RequestBody UserGroupRequestBody requestbody) {
        String groupId = requestbody.getGroupId();
        if (StringUtil.isEmpty(groupId)) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        UserGroup byId = userGroupService.findById(groupId);
        if (null != byId) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), byId);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 判断分组是否存在
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/getByName")
    public ResultBody getByName(@RequestBody UserGroupRequestBody requestBody) {
        List<UserGroup> byName = userGroupService.findByName(requestBody.getGroupName());
        if (null != byName && byName.size() > 0) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
    }

    /**
     * 查看所有分组列表
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/query")
    public ResultBody getGroupList(@RequestBody UserGroupRequestBody requestBody) {
        Integer page = requestBody.getPage() == null ? 0 : requestBody.getPage();
        Integer size = requestBody.getSize() == null ? 10 : requestBody.getSize();
        requestBody.setPage(page);
        requestBody.setSize(size);
        PageDTO<UserGroup> allGroup = null;
        try {
            allGroup = userGroupService.findAllGroup(requestBody);
        } catch (UserGroupException e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), allGroup);
    }

    /**
     * 分组添加会员
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/select_user")
    public ResultBody selectMatchingUsers(@RequestBody UserGroupRequestBody requestBody) {
        String groupId = requestBody.getGroupId();
        List<String> userIds = requestBody.getUserIds();
        if (userIds.size() <= 0 || StringUtil.isEmpty(groupId)) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        try {
            String groupRelationShip = userGroupService.createGroupRelationShip(groupId, userIds);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), groupRelationShip);
        } catch (Exception e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }

    /**
     * 根据分组id查询所有分组成员
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/query_group_member")
    public ResultBody queryGroupMember(@RequestBody UserGroupRequestBody requestBody) {

        List<String> userIds = userGroupService.findAllmemberByGroupId(requestBody);
        if (userIds.size() > 0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), userIds);
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    @PostMapping("/delete_group")
    public ResultBody removeGroup(@RequestBody UserGroupRequestBody requestBody) {
        String groupId = requestBody.getGroupId();
        String msg = userGroupService.removeGroup(groupId);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), msg, msg);
    }

    /**
     * 分组推送优惠券
     *
     * @return
     */
    @PostMapping("/group_push_coupon")
    public ResultBody pushCouponFromGroup(@RequestBody UserGroupRequestBody requestBody) {
        String msg = userGroupService.pushCoupomForGroupMember(requestBody);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), msg, msg);
    }

    /**
     * 查看分组用户
     *
     * @return
     */
    @PostMapping("/query_condition")
    public ResultBody queryCondition(@RequestBody UserGroupRequestBody requestBody) {
        PageDTO<UserDTO> pageDTO = userGroupService.queryCondition(requestBody);
        if (pageDTO==null||pageDTO.getList().size()<=0){
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), pageDTO);
    }
}
